<?php
/**
* @version		$Id: view.html.php 10711 2008-08-21 10:09:03Z eddieajau $
* @package		Joomla
* @subpackage	Survey
* @copyright	Copyright (C) 2005 - 2008 Open Source Matters. All rights reserved.
* @license		GNU/GPL, see LICENSE.php
* Joomla! is free software. This version may have been modified pursuant
* to the GNU General Public License, and as distributed it includes or
* is derivative of works licensed under the GNU General Public License or
* other free or open source software licenses.
* See COPYRIGHT.php for copyright notices and details.
*/

// Check to ensure this file is included in Joomla!
defined('_JEXEC') or die( 'Restricted access' );

jimport( 'joomla.application.component.view');

/**
 * HTML View class for the Survey component
 *
 * @static
 * @package		Joomla
 * @subpackage	Survey
 * @since 1.0
 */
class SurveyViewSurvey extends JView
{
	function display($tpl = null)
	{
		global $mainframe;

		$db 	  =& JFactory::getDBO();
		$document =& JFactory::getDocument();
		$pathway  =& $mainframe->getPathway();

		$survey_id = JRequest::getVar( 'id', 0, '', 'int' );
		$part_id = JRequest::getVar( 'part', 1, '', 'int' );
		$vote_id = JRequest::getVar( 'voteid', 0, '', 'int' );
		
		$survey =& JTable::getInstance('survey', 'Table');
		$survey->load( $survey_id );

		// if id value is passed and survey not published then exit
		if ($survey->id > 0 && $survey->published != 1) {
			JError::raiseError( 403, JText::_('Access Forbidden') );
			return;
		}

		// Adds parameter handling
		$params = $mainframe->getParams();

		//Set page title information
		$menus	= &JSite::getMenu();
		$menu	= $menus->getActive();

		// because the application sets a default page title, we need to get it
		// right from the menu item itself
		if (is_object( $menu )) {
			$menu_params = new JParameter( $menu->params );
			if (!$menu_params->get( 'page_title')) {
				$params->set('page_title',	$survey->title);
			}
		} else {
			$params->set('page_title',	$survey->title);
		}
		$document->setTitle( $params->get( 'page_title' ) );

		//Set pathway information
		$pathway->addItem($survey->title, '');

		$params->def( 'show_page_title', 1 );
		$params->def( 'page_title', $survey->title );

		// Check if there is a survey corresponding to id and if survey is published
		if ($survey->id > 0)
		{
			if (empty( $survey->title )) {
				$survey->id = 0;
				$survey->title = JText::_( 'Select Survey from the list' );
			}

			$query = 'SELECT a.id, a.text, a.type '
				. ' FROM #__survey_data AS a'
				. ' WHERE a.survey_id = '. (int) $survey->id
				. ' AND a.part = '. (int) $part_id
				. ' ORDER BY a.id';
			$db->setQuery( $query );
			$votes = $db->loadObjectList();
			
		} else {
			$votes = array();
		}

		$graphwidth = 0;
		$startId = 0;
		$endId = 0;
		$lists = array();
		if (count( $votes ) <= 0){
			$lists['survey_title'] = '<p>答题完成，非常感谢您的参与！</p>';
		}
		else{
			$query = 'SELECT part, text '
				. ' FROM #__survey_desc '
				. ' WHERE survey_id = '. (int) $survey->id
				. ' AND (part = 0 OR part = '. (int) $part_id .')'
				. ' ORDER BY part';
			$db->setQuery( $query );
			$part_text = $db->loadObjectList();
			for ($i = 0; $i < count( $part_text ); $i++){
				if ($part_text[$i]->part == 0)
					$lists['survey_title'] = $part_text[$i]->text;
				else
					$lists['part_title'] = $part_text[$i]->text;
			}
			
			$k = 0;
			for ($i = 0; $i < count( $votes ); $i++)
			{
				$vote =& $votes[$i];
				if ($vote->type == 'radio' || $vote->type == 'radio_other')
				{
		  			$query = 'SELECT a.id, a.text, a.val '
						. ' FROM #__survey_option a, #__survey_data_option_map map'
						. ' WHERE a.id = map.option_id'
						. ' AND map.data_id = '. (int) $vote->id
						. ' ORDER BY a.id';
					$db->setQuery( $query );
					$vote->option = $db->loadObjectList();
				} else {
					$vote->option = array();
				}
								
				if (count($vote->option) > $k)
				{
					$k = count($vote->option);
				}
				if($i == 0)
					$startId = $vote->id;
				if($i == count( $votes ) - 1)
					$endId = $vote->id;
			}
			$graphwidth = $k + 1;
		}

		$this->assign('graphwidth',	$graphwidth);
		$this->assign('survey_id',	$survey_id);
		$this->assign('part_id',	$part_id);
		$this->assign('startId',	$startId);
		$this->assign('endId',	$endId);
		$this->assign('voteid',	$vote_id);
		
		$this->assignRef('lists',	$lists);
		$this->assignRef('params',	$params);
		$this->assignRef('survey',	$survey);
		$this->assignRef('votes',	$votes);

		parent::display($tpl);
	}
}
?>
